﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

using EnvDTE;
using EnvDTE100;

namespace Artisteer_Project_Converter.VisualStudioApi
{
    public class VsDocument
    {
        private Document document = null;

        /// <summary>
        /// Create project instance
        /// </summary>
        /// <param name="solution">Project object</param>
        internal VsDocument(Document document)
        {
            this.document = document;
        }

        /// <summary>
        /// Auto format text document
        /// </summary>
        public void AutoFormat()
        {
          TextDocument document = (TextDocument)this.document.Object();

          document.StartPoint.CreateEditPoint().SmartFormat(document.EndPoint);
        }

        /// <summary>
        /// Save document
        /// </summary>
        public void Save()
        {
            while (true)
            {
                try
                {
                    document.Save();
                    break;
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }

        /// <summary>
        /// Close document
        /// </summary>
        public void Close()
        {
            while (true)
            {
                try
                {
                    document.Close();
                    break;
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }
    }
}
